using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.IO;
namespace Soyuz5.Utility
{
    public static class WebControl
    {
        /// <summary>
        /// Find control by id
        /// </summary>
        /// <param name="control"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Control GetControlByID(Control control, string id)
        {
            Control roundCtl = control.FindControl(id);
            if (roundCtl != null)
            {
                return roundCtl;
            }

            foreach (Control child in control.Controls)
            {
                Control result = GetControlByID(control, id);
                if (result != null)
                    return result;
            }

            return null;
        }

        /// <summary>
        /// Find controls by type
        /// </summary>
        /// <param name="control"></param>
        /// <param name="controlType"></param>
        /// <returns></returns>
        public static IEnumerable<Control> GetControlByType(Control control, Type controlType)
        {
            List<Control> list = new List<Control>();
            _getControlByType(list, control, controlType);
            return list;

        }

        /// <summary>
        /// Recursive search for controls
        /// </summary>
        /// <param name="list"></param>
        /// <param name="control"></param>
        /// <param name="controlType"></param>
        static void _getControlByType(List<Control> list, Control control, Type controlType)
        {
            if (control.GetType().IsAssignableFrom(controlType))
            {
                list.Add(control);
            }
            foreach (Control child in control.Controls)
            {
                _getControlByType(list, child, controlType);
            }
        }


        /// <summary>
        /// Loads a user control and adds it to a dummy page
        /// </summary>
        /// <param name="path"></param>
        public static UserControl LoadUserControl(string path)
        {
            Page pageHolder = new Page();
            UserControl userControl = (UserControl)pageHolder.LoadControl(path);
            pageHolder.Controls.Add(userControl);
            return userControl;
        }

        /// <summary>
        /// Render a user control to a string, control must belong to a page
        /// </summary>
        /// <param name="userControl"></param>
        /// <returns></returns>
        public static string RenderPage(Page page)
        {
            StringWriter output = new StringWriter();
            System.Web.HttpContext.Current.Server.Execute(page, output, false);
            return output.ToString();
        }

        /// <summary>
        /// Render a web control to a string
        /// </summary>
        /// <param name="userControl"></param>
        /// <returns></returns>
        public static string RenderControl(System.Web.UI.WebControls.WebControl control)
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
                {
                    control.RenderControl(textWriter);
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// Render a html control to a string
        /// </summary>
        /// <param name="userControl"></param>
        /// <returns></returns>
        public static string RenderControl(System.Web.UI.HtmlControls.HtmlControl control)
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
                {
                    control.RenderControl(textWriter);
                }
            }
            return sb.ToString();
        }
    }
}
